﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Obsidian.Utils;
using Obsidian.Data.Sql;

namespace Obsidian.Runtime
{
    public class Logger
    {

        public static string Log(string source, string info)
        {
            try
            {
                string errorCode = StringUtil.CreateRandomCode(10);
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                string logPath = RuntimeUtil.BasePath + "\\log\\" + source;
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                logPath += "\\" + today + ".log";
                if (!File.Exists(logPath))
                {
                    FileStream stream = File.Create(logPath);
                    stream.Close();
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("日志! " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ====================================");
                sb.AppendLine("日志码：" + errorCode);
                sb.AppendLine(info);
                sb.AppendLine("");
                sb.AppendLine("");
                IOUtil.WriterTextToFile(logPath, true, sb.ToString());

                return errorCode;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Track(string info)
        {
            string errorCode = StringUtil.CreateRandomCode(10);
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string logPath = RuntimeUtil.BasePath + "/log/" + today + ".track";
            if (!File.Exists(logPath))
            {
                FileStream stream = File.Create(logPath);
                stream.Close();
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("日志! " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ====================================");
            sb.AppendLine("日志码：" + errorCode);
            sb.AppendLine(info);
            sb.AppendLine("");
            sb.AppendLine("");
            IOUtil.WriterTextToFile(logPath, true, sb.ToString());

            return errorCode;
        }

        public static string Info(string info)
        {
            string errorCode = StringUtil.CreateRandomCode(10);
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string logPath = RuntimeUtil.BasePath + "/log/" + today + ".txt";
            if (!File.Exists(logPath))
            {
                FileStream stream = File.Create(logPath);
                stream.Close();
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("日志! " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ====================================");
            sb.AppendLine("日志码：" + errorCode);
            sb.AppendLine(info);
            sb.AppendLine("");
            sb.AppendLine("");
            IOUtil.WriterTextToFile(logPath, true, sb.ToString());

            return errorCode;
        }

        public static string Warning(string info)
        {
            string errorCode = StringUtil.CreateRandomCode(10);
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string logPath = RuntimeUtil.BasePath + "/log/" + today + ".txt";
            if (!File.Exists(logPath))
            {
                FileStream stream = File.Create(logPath);
                stream.Close();
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("警告！ " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ====================================");
            sb.AppendLine("日志码：" + errorCode);
            sb.AppendLine(info);
            sb.AppendLine("");
            sb.AppendLine("");
            IOUtil.WriterTextToFile(logPath, true, sb.ToString());

            return errorCode;
        }

        public static string Error(string message)
        {
            return Error(null, message);
        }

        public static string Error(Exception ex)
        {
            return Error(ex, null);
        }

        public static string Error(Exception ex, string message)
        {

            string errorCode = StringUtil.CreateRandomCode(10);
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string logPath = RuntimeUtil.BasePath + "/log/" + today + ".txt";
            if (!File.Exists(logPath))
            {
                FileStream stream = File.Create(logPath);
                stream.Close();
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("错误！ " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ====================================");
            sb.AppendLine("错误码：" + errorCode);

            if (!String.IsNullOrEmpty(message))
            {
                sb.AppendLine("自定义信息：");
                sb.AppendLine(message);
            }
            bool first = true;
            while (ex != null)
            {
                if (first) first = false;
                else sb.AppendLine("　-----------");
                sb.AppendLine("错误信息：" + ex.Message);
                sb.AppendLine("错误来源：" + ex.Source);
                sb.AppendLine("错误方法：" + ex.TargetSite);
                sb.AppendLine("堆栈信息：");
                sb.AppendLine(ex.StackTrace);
                ex = ex.InnerException;
            }
            sb.AppendLine("");
            sb.AppendLine("");
            IOUtil.WriterTextToFile(logPath, true, sb.ToString());
            return errorCode;
        }

    }
}
